//
//  VCOfClassifyTest.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/9.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfClassifyTest.h"
// 这个 cell 一样, 就直接拿过来用了
#import "TVCellOfBelowInformationDoTest.h"
#import "BaseTableView.h"
#import "MJMacro.h"
#import "ZWYNetTool.h"
// model 也一样的, 拿出来用
#import "DoTestBelowInformationData.h"
#import "DoTestBelowInformationRootClass.h"
#import "UIImageView+WebCache.h"
#import "VCOfBeginDoTest.h"
#import "MJRefresh.h"
@interface VCOfClassifyTest ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) DoTestBelowInformationRootClass *doTestBelowInformationRootClass;
@property (nonatomic, strong) BaseTableView *tableViewOfClassifyTest;
@property (nonatomic, strong) NSMutableArray<DoTestBelowInformationData *> *mArrayOfData;
@end

@implementation VCOfClassifyTest

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 数据处理
    [self handleData];
    // 创建 tableView
    [self createTableView];
}
#pragma mark - 数据处理
- (void)handleData{
    NSString *stringOfCategory_id = [NSString stringWithFormat:@"http://yiapi.xinli001.com/yi/test-list.json/?category_id=%@&limit=10&offset=0&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&_version=4.0&evn=1&sign=2a6c2f55be7817d6152fa60491a488b7&token=0d4eeb8732f9ae2c73401002570ceb93", _category_id];
    [ZWYNetTool GET:stringOfCategory_id andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _doTestBelowInformationRootClass = [DoTestBelowInformationRootClass modelDic:result];
        _mArrayOfData = _doTestBelowInformationRootClass.data.mutableCopy;
    [self.tableViewOfClassifyTest reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableViewOfClassifyTest = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableViewOfClassifyTest];
    self.tableViewOfClassifyTest.delegate = self;
    self.tableViewOfClassifyTest.dataSource = self;
    [self.tableViewOfClassifyTest registerNib:[UINib nibWithNibName:@"TVCellOfBelowInformationDoTest" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfBelowInformationDoTest"];
    // 下拉刷新
    self.tableViewOfClassifyTest.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 上拉加载
    self.tableViewOfClassifyTest.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
#pragma mark - 上拉刷新
- (void)loadNewData{
    [self handleData];
    [self.tableViewOfClassifyTest.mj_header endRefreshing];
}
#pragma mark - 下拉加载
- (void)loadMoreData{
    static int i = 5;
    NSString *stringOfCategory_id = [NSString stringWithFormat:@"http://yiapi.xinli001.com/yi/test-list.json/?category_id=%@&limit=10&offset=%d&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&_version=4.0&evn=1&sign=2a6c2f55be7817d6152fa60491a488b7&token=0d4eeb8732f9ae2c73401002570ceb93", _category_id, i];
    [ZWYNetTool GET:stringOfCategory_id andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _doTestBelowInformationRootClass = [DoTestBelowInformationRootClass modelDic:result];
        for (DoTestBelowInformationData *doTestBelowInformationData in _doTestBelowInformationRootClass.data) {
            [_mArrayOfData addObject:doTestBelowInformationData];
        }
        [self.tableViewOfClassifyTest reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [self.tableViewOfClassifyTest.mj_footer endRefreshing];
    i = i + 5;
    
}
#pragma mark - tableView 代理方法 row 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mArrayOfData.count;

}
#pragma mark - tableView 代理方法 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 7;
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfBelowInformationDoTest *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfBelowInformationDoTest" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_mArrayOfData[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
    
    cell.titleLabel.text = _mArrayOfData[indexPath.row].title;
    cell.viewnumLabel.text = _mArrayOfData[indexPath.row].viewnum;
    cell.commentnumLabel.text = _mArrayOfData[indexPath.row].commentnum;
    return cell;
}
#pragma mark - tableView 的点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfBeginDoTest *vcOfBeginDoTest = [[VCOfBeginDoTest alloc] init];
    vcOfBeginDoTest.coverString = _mArrayOfData[indexPath.row].cover;
    vcOfBeginDoTest.titleString = _mArrayOfData[indexPath.row].title;
    vcOfBeginDoTest.contentString = _mArrayOfData[indexPath.row].content;
    vcOfBeginDoTest.viewnumString = _mArrayOfData[indexPath.row].viewnum;
    vcOfBeginDoTest.myID = _mArrayOfData[indexPath.row].ID;
    [self presentViewController:vcOfBeginDoTest animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
